package cn.touna.jss.modules.third.zhishu.support;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Objects;


public class SHAUtil {

	private static final String TYPE = "SHA-256";
	
	public static String toSHA(String message) {

		if (message == null || Objects.equals("", message.trim()))
			return null;

		try {
			MessageDigest digest = MessageDigest.getInstance(TYPE);
			if (digest != null) {
				
				byte[] msgs = message.getBytes();
				digest.update(msgs);
				byte[] digestMsgs = digest.digest();
				
				String result = getDigestStr(digestMsgs); 
				return result;
			}
			
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}
		
		return null;
	 }
	
	 private static String getDigestStr(byte[] origBytes) {
		 
	        String tempStr = null;
	        StringBuilder builder = new StringBuilder();
	        for (int i = 0; i < origBytes.length; i++) {

	            tempStr = Integer.toHexString(origBytes[i] & 0xff);
	            if (tempStr.length() == 1) {
	                builder.append("0");
	            }
	            builder.append(tempStr);

	        }
	        return builder.toString();
	 }


}
